package rc.work.module.infra.controller.admin.onlyoffice;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import rc.work.framework.common.pojo.CommonResult;
import rc.work.module.infra.service.onlyoffice.OnlyOfficeService;


import java.net.MalformedURLException;

import static rc.work.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - onlyoffice")
@RestController
@RequestMapping("/infra/office")
@Validated
public class OnlyOfficeController {
    @Resource
    private OnlyOfficeService officeService;

    @RequestMapping("/save")
    public String save(@RequestParam(name = "fileId")Long fileId,
                                     @RequestParam(name = "fileType",defaultValue = "0") String fileType,
                                     @RequestParam(name = "userId",defaultValue = "0") Long userId,
                                     @RequestParam(name = "userName",defaultValue = "0") String userName,HttpServletRequest request, HttpServletResponse response) {
        return officeService.save(fileId,fileType,userId,userName,request,response);
    }

    @GetMapping("/getConfig")
    public CommonResult<?> getConfig(@RequestParam(name = "fileId")Long fileId,
                               @RequestParam(name = "fileType",defaultValue = "0") String fileType, HttpServletRequest request) {
        return success(officeService.createConfig(fileId,fileType,request));
    }

    @GetMapping("/getFile")
    public CommonResult<ResponseEntity<org.springframework.core.io.Resource>> getFile(@RequestParam(name = "fileId")Long fileId,
                                                                                      @RequestParam(name = "fileType",defaultValue = "0") String fileType) throws MalformedURLException {
        return success(officeService.getFile(fileId,fileType));
    }



    @RequestMapping("/saveNew")
    public String saveNew(@RequestParam(name = "fileId")Long fileId,
                       @RequestParam(name = "fileType",defaultValue = "0") String fileType,
                       @RequestParam(name = "userId",defaultValue = "0") Long userId,
                       @RequestParam(name = "userName",defaultValue = "0") String userName,HttpServletRequest request, HttpServletResponse response) {
        return officeService.saveNew(fileId,fileType,userId,userName,request,response);
    }

    @GetMapping("/getConfigNew")
    public CommonResult<?> getConfigNew(@RequestParam(name = "fileId")Long fileId,
                                     @RequestParam(name = "fileType",defaultValue = "0") String fileType, HttpServletRequest request) {
        return success(officeService.createConfigNew(fileId,fileType,request));
    }

    @GetMapping("/getFileNew")
    public CommonResult<ResponseEntity<org.springframework.core.io.Resource>> getFileNew(@RequestParam(name = "fileId")Long fileId,
                                                                                      @RequestParam(name = "fileType",defaultValue = "0") String fileType) throws MalformedURLException {
        return success(officeService.getFileNew(fileId,fileType));
    }


}